package com.work.cos.util;

import java.util.Map;

import org.apache.http.HttpStatus;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * @author intel
 * 
 * 短信
 *
 */
public class SMSUtils {
	
	private static Logger LOGGER = LoggerFactory.getLogger(SMSUtils.class);
	
	private static String url = "http://sms.zh-ink.com/api/sms/send"; 
	private static String userName = "mflzd";
	private static String userpwd = MD5Util.MD5Encode("pefr68", "UTF-8").toLowerCase();
	
	/**
	 * 
	 * @param mobile
	 * @param verCode
	 */
	public static Map<String,Object> sendMessage(String mobile){
		try {
			
			int verCode = (int)((Math.random()*9+1)*10000);
			JSONObject params = new JSONObject();
			params.put("uid", userName);
			params.put("userpwd", userpwd);
			params.put("mobile", mobile);
			params.put("message", "【安必信】验证码：" + verCode + "，感谢你的支持！");
			
			String[] res = HttpClientUtil.doPostJson(url, params.toString());
			
			if (Integer.parseInt(res[0]) != HttpStatus.SC_OK){
				
				LOGGER.error("send message error ....message = {}", "网络访问不通!");
				return null;
			}else {
				
				LOGGER.info("send message success ... msg={}",res[1] );
				Map<String,Object> result = new ObjectMapper().readValue(res[1], Map.class);
				result.put("verCode", verCode);
				result.put("content", "【安必信】验证码：{param0}，感谢你的支持！");
				return result;
			}
			
		} catch (Exception e) {
			
			LOGGER.error("send message error ....message = {}", e.getMessage());
			return null;
		}
	}
}
